import 'dart:ffi';

import 'package:flutter/material.dart';

class SingleSelectInputField extends StatefulWidget {



  const SingleSelectInputField({super.key, required this.title, required this.options, required this.label, required this.onValueSelected});
  final String title;
  final String label;
  final List<String> options;
  final ValueChanged<int> onValueSelected;
  @override
  _SingleSelectInputFieldState createState() => _SingleSelectInputFieldState();
}

class _SingleSelectInputFieldState extends State<SingleSelectInputField> {
  late List<String> _options; // 可选项数据
  String? _selectedValue; // 选中的值
  final TextEditingController _controller = TextEditingController();

  @override
  initState(){
    super.initState();
    _options = widget.options;
  }

  // 弹出选择对话框
  Future<void> _showSelectionDialog() async {
    final String? result = await showDialog<String>(
      context: context,
      builder: (context) => SimpleDialog(
        title: Text(widget.title),
        children: _options.map((option) => SimpleDialogOption(
          onPressed: () => Navigator.pop(context, option),
          child: Text(option),
        )).toList(),
      ),
    );

    if (result != null) {
      setState(() {
        _selectedValue = result;
        _controller.text = result;
        widget.onValueSelected(widget.options.indexOf(result));
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: _controller,
      decoration: InputDecoration(
        labelText: '${widget.label}',
        suffixIcon: Icon(Icons.arrow_drop_down),
        border: OutlineInputBorder(),
      ),
      readOnly: true,
      onTap: _showSelectionDialog,
      validator: (value) => value?.isEmpty ?? true ? '请选择' : null,
    );
  }
}